package com.jsyl.dao.authority;

import com.jsyl.dao.base.BaseDao;
import com.jsyl.dao.base.SqlBuilder;
import com.jsyl.entity.authority.ClassSchedule;
import com.jsyl.infra.utils.page.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.*;


/**
 *
 *
 */
@Repository
public class CenterDao extends BaseDao<ClassSchedule> {
    @Override
    public PageModel<ClassSchedule> query(PageModel<ClassSchedule> pm, ClassSchedule classSchedule) {
        StringBuilder sql = new StringBuilder();
        StringBuilder totalsql = new StringBuilder();
        StringBuilder whereSql = new StringBuilder();
        sql.append(SqlBuilder.SQL_SELECT).append("center_name,center_id").append(SqlBuilder.SQL_FROM);
        totalsql.append(SqlBuilder.SQL_SELECT).append("count(1) from (select distinct center_name ").append(SqlBuilder.SQL_FROM);
        whereSql.append(
                "class_schedule_detail")
                .append(SqlBuilder.SQL_WHERE).append("1=1 ");

        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(classSchedule.getCenterName())) {
            whereSql.append(SqlBuilder.SQL_AND).append("center_name").append(SqlBuilder.SQL_LIKE).append(":centerName");
            paramMap.put("centerName", "%" + classSchedule.getCenterName() + "%");
        }


        totalsql.append(whereSql).append(" ) a");
        whereSql.append(" group by center_id");
        logger.info("totalsql : " + "\"" + totalsql.toString() + "\"");
        Integer totalCount = namedTemplate.queryForObject(totalsql.toString(), paramMap, Long.class).intValue();


        Integer startIndex = pm.getStartIndex();
        Integer pageSize = pm.getPageSize();

        if (pageSize == null || pageSize <= 0) {
            pageSize = PageModel.DEFAULT_PAGE_SIZE;
        }

        if (startIndex == null || startIndex < 0) {
            startIndex = 0;
        } else if (startIndex > totalCount) {
            Integer totalPage = (int) Math.ceil(totalCount.doubleValue() / pageSize.doubleValue());
            startIndex = (totalPage - 1) * pageSize;
        }

        whereSql.append(SqlBuilder.SQL_LIMIT);
        if (startIndex != null && startIndex >= 0) {
            whereSql.append(startIndex).append(",");
        }
        whereSql.append(pageSize);

        sql.append(whereSql);

        logger.info("SQL : " + "\"" + sql.toString() + "\"");
        List<Map<String, Object>> resultList = namedTemplate.queryForList(sql.toString(), paramMap);
        Map<String, String> columnMapper = new HashMap<>();
        List<ClassSchedule> list = new ArrayList<ClassSchedule>();
        ClassSchedule vo = null;
        for (Iterator<Map<String, Object>> iterator = resultList.iterator(); iterator.hasNext(); ) {
            Map<String, Object> record = iterator.next();
            vo = new ClassSchedule();
            String centerId = "";
            String centerName = "";


            if (!StringUtils.isEmpty((String) record.get("center_id"))) {
                centerId = (String) record.get("center_id");
            }
            if (!StringUtils.isEmpty((String) record.get("center_name"))) {
                centerName = (String) record.get("center_name");
            }

            vo.setCenterName(centerName);
            vo.setCenterId(centerId);
            list.add(vo);
        }
        pm.setTotalCount(totalCount);
        pm.setPageData(list);

        return pm;

    }

}
